<template>
    <div class="sys-page">
        <app-title title="密码有效期设置" style="display:inline;"></app-title>
        <el-button type="success"  @click="refresh()" round style="float:right; margin-top:10px;" icon="el-icon-refresh">刷新</el-button>
        <el-table v-loading="loading" border :data="tableData.body" :row-class-name="tableRowClassName">
            <el-table-column prop="content" align="center" label="有效期(天)"></el-table-column>
            <el-table-column label="操作" align="center">
                <template slot-scope="scope">
                    <el-button type="text" size="small" @click="edit">修改</el-button>
                </template>
            </el-table-column>
        </el-table>


        <el-dialog :title="dialogTitle" :visible.sync="editTableShow" width='30%' :fullscreen='false'>
            <el-form  :rules="validateRules">
                <el-form-item prop="selectVal" label="密码有效期设置(天)">
                     <el-select v-model="selectVal" placeholder="1">
                    <el-option
                    v-for="item in 180"
                    :key="item"
                    :label="item"
                    :value="item">
                    </el-option>
                </el-select>   
                </el-input>
                </el-form-item>
            </el-form>
            <el-button @click.native.prevent="ok" :disabled="okEnable">确定</el-button>
            <el-button @click.native.prevent="cancel">取消</el-button>

        </el-dialog>
    </div>

</template>
<style>
    .el-table .warning-row {
        background: #F08080;
    }

    .el-table .success-row {
        background:#f0f9eb;
    }
</style>
<script>
    export default {
        name: 'passwordexpirationpolicy',
        data(){
            var validNumber= (rule, val, callback) => {
                console.log("in invalidNum "+val)
                var value = this.selectVal
                var pattern = /^[1-9]\d*$/
                if(value === ''){
                    this.okEnable = true;
                    callback(new Error("请输入有效期值"))
                }else if(pattern.test(value) == false){
                    this.okEnable = true;
                    callback(new Error("请输入数字"))
                }else {
                    this.okEnable = false;
                    callback()
                }
            }
            return {
                validateRules:{
                    selectVal:[{ validator: validNumber, trigger: 'change', required: true, message:"请输入数字"}]
                },
                okEnable: true,
                dialogTitle:"密码有效期设置",
                failcnt:1,
                loading: false,
                editTableShow: false,
                selectVal:1,

                lastValue: 0,
                tableData:{
                    body:[{content: ''}],
                    head:[],
                    loading:true
                },

                errorMsg:"您的信息有误，请联系管理员修改",
                issafe:true

            }
        },

        mounted(){
            this.query()
        },
        methods:{
            refresh(){
            this.query()
        },
            tableRowClassName({row, rowIndex}) {
                if(this.issafe === false){
                    return 'warning-row';
                }else{
                    return 'success-row'
                }
            },

            edit(){
                this.lastValue = this.tableData.body[0].content
                this.selectVal = this.tableData.body[0].content
                this.editTableShow = true
            },
            ok(){
                this.editTableShow = false
                this.HTTP.methods.apiPost("/sysconfig/b0004.shtml",
                    {content:this.selectVal,id: this.tableData.body[0].id }).
                then(res =>
                {
                    if (res.status != 200) {
                    this.$message(res.msg);
                    // this.GLOBAL._g.toastMsg('error', res.msg)
                } else {
                    this.tableData.body[0] = this.selectVal;
                    this.$message("设置成功");
                    // this.GLOBAL._g.toastMsg('success', '修改成功')
                }
                this.loading = false;
            })
            },
            cancel(){
                this.tableData.body[0].content = this.lastValue
                this.editTableShow = false


            },
            query(){
                this.HTTP.methods.apiGet("/sysconfig/b0004", {}).then(res =>
                {
                    if(res.code == 200){
                    this.tableData.body[0].content = (res.data.content)
                    this.issafe = res.data.safe
                }else{
                    this.tableData.body = []
                }
            })
            }
        }
    }
</script>


